Linear fits using fit_pandas_GUI()

You can try this notebook live by lauching it in Binder.This can take a while to launch, be patient. Binder.

First we import pandas, numpy and pandas_GUI and then create a noisy line to fit.

Then make a quick plot using plot_pandas_GUI() to see what the data looks like. See the Pandas GUI Website for examples and documentation on using the plotting GUI.

Figure 1: Plot of the noisy linear data. Since the noise is generated using a normal distribution from a random number generator the data will look different each time the notebook is restarted and run.

Example 1: Basic linear fit

The fit was done by running the command fit_pandas_GUI() in an empty code cell.

1. On the first tab

the data set, which columns contained the X- and Y-data and a name for the trace were chosen.

Fit_tab_1

2. On the second tab

the the default 'none' value was kept for uncertainties.

Fit_tab_2

3. On the third tab

the linear model was chosen and all the boxes were left in their default state.

Fit_tab_3

4. The fourth tab was skipped

because we fit the whole range of data.

5. On the fifth tab

labels for the X and Y axis were input and the Display Mirror Axes box was checked.

Fit_tab_5

6. On the last (sixth) tab

the final checks were done and then the 'Do Fit' button was clicked, closing the GUI and running the code in the cell below to perform the fit and display the results.

Fit_tab_6

Figure 2: The results of fitting the noisy linear data to a line using default settings. Alternative formatting of line styles, markers, etc... can be accessed by editing the code produced by the GUI. Overall plot styling can be adjusted on tab 5.

Example 2: Linear fit with known errors in the measurements

Errors can be a constant value (same on all data points), a percentage of each value or a value provided in a column of the pandas dataframe. For this example we will use a constant that is a little bigger than the random error used to generate the data. This causes the uncertainties in the fit parameters to increase.

The only change from the steps in example 1 is on tab 2 where the Error Type is changed to "constant" and the value in the % or constant box is set to "0.7". It is important to click outside this box to register the updated value.

Fit_line_with_errors_tab3

Figure 3: Results of fitting the noisy line while accounting for assigned errors in the values. This changes the uncertainty in the fitted parameters. In this case the y-intercept cannot really be distinguished from zero. Note: if the data set is dense the default display of error bars can make it hard to see what is happening. The error bars can be removed, while still accounting for them in the fit, by deleting error_y_type="data", error_y_array=Yerr where they appear in the scat= statements.

Example 3: Fit to a portion of the data

A range or multiple ranges of the data set to fit may be selected using the optional tab 4. Consecutive pairs of selected points starting with the lowest data index number define each range. Points can be deselected by holding down the ctrl key while clicking on a point.

For this example we have selected a range in the middle of the plot and extended the fit so that the fit and residuals are extrapolated across the whole data range as shown in the image below.

Fit_line_range

Figure 4: Fit to only the region with the solid fit line. If the Extend fitted function plot box had not been checked the dashed line and residuals outside the fit region would not be plotted.

Learn More

In addition to trying it below if this is a live notebook, you can look at the other examples listed in the Pandas GUI website.

Try It

If you are running this notebook live in binder you can try it here by running the first cell to import the tools and create the data. Then run the cell below to create the GUI. Note: You may want to expand the collapsed instructions to learn more about each tab.